package hong.jun.pet_home.controller.user;


import hong.jun.APP;
import hong.jun.basic.util.AjaxResult;
import hong.jun.basic.util.PageList;
import hong.jun.pet_home.domain.user.User;
import hong.jun.pet_home.query.EmployeeQuery;
import hong.jun.pet_home.service.user.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * restfull风格
 *
 * @Controller
 * @ResponseBody
 */

@RestController
@RequestMapping("/user")
@Api(tags = "UserController", description = "UserController | 用户管理接口")
//@CrossOrigin//后台的跨域解决方案-spring4.2.5及以上版本支持
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private IUserService userService;

    @ApiOperation(value = "saveOrUpdate方法", notes = "有无id决定新增还是修改方法")
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody User user) {
        //判断有无id决定新增还是修改
        try {
            if (user.getId() != null) {
                userService.update(user);
            } else {
                userService.save(user);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(String.format("user新增或修改失败 user: %d ", user));
            return new AjaxResult(e.toString());
        }
    }

    /**
     * 根据id 删除
     */
    @ApiOperation(value = "del 方法", notes = "单个user删除方法")
    @DeleteMapping(value = "/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        try {
            User user = userService.getById(id);
            System.out.println("删除" + user.toString());//未查到对象为空会报空指针异常
            userService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.toString());
        }
    }

    //根据id 批量删除
    @ApiOperation(value = "dels 方法", notes = "批量user删除方法")
    @PostMapping("/dels")
    public AjaxResult dels(@RequestBody Long[] ids) {
        try {
            userService.deletes(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.toString());
        }
    }

    /*根据Id查询*/
    @ApiOperation(value = "getById 方法", notes = "单个user查询方法")
    @GetMapping(value = "/{id}")
    public User getById(@PathVariable("id") Long id) {
        return userService.getById(id);
    }

    @ApiOperation(value = "getAll 方法", notes = "所有user查询方法")
    @GetMapping
    public List<User> getAll() {
        return userService.getAll();
    }

    /*条件分页查询*/
    @ApiOperation(value = "getPage 方法", notes = "user分页查询方法")
    @PostMapping
    public PageList<User> getPage(@RequestBody EmployeeQuery query) {
        return userService.queryPage(query);
    }
}
